package com.jsle.financial.vo;

import java.util.List;

/**
 * 分页实体
 * @author wisdying
 * @date 2012-10-26
 */
public class Page {
	public static final int DEFAULT_PAGE_SIZE = 10;
	/**起始页*/
    private int pageIndex;
    /**页大小*/
    private int pageSize;
    /**总条数*/
    private int totalCount;
    /**页数*/
    private int pageCount;
    /**数据集*/
    private List data;
    
    public Page(){
    	
    }
    public Page(int pageIndex, int totalSize, int pageSize, List data) {
		this.pageSize = pageSize;
		this.pageIndex = pageIndex;
		this.totalCount = totalSize;
		setPageCount(totalSize / pageSize + (totalSize % pageSize != 0 ? 1 : 0));
		this.data = data;
	}
    
    public Page(int pageIndex, int pageSize){
        if(pageIndex < 1)
            pageIndex = 0;
        if(pageSize < 1)
            pageSize = 1;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    public static int getStartOfPage(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}
    
    public Page(int pageIndex){
        this(pageIndex, 10);
    }

    public int getPageIndex(){
        return pageIndex;
    }

    public int getPageSize(){
        return pageSize;
    }

    public int getPageCount(){
        return pageCount;
    }

    public int getTotalCount(){
        return totalCount;
    }

    public int getFirstResult(){
        return (pageIndex - 1) * pageSize;
    }

    public boolean getHasPrevious(){
        return pageIndex > 1;
    }

    public boolean getHasNext(){
        return pageIndex < pageCount;
    }

    public List getData() {
		return data;
	}
	public void setData(List data) {
		this.data = data;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public void setTotalCount(int totalCount){
        this.totalCount = totalCount;
        pageCount = totalCount / pageSize + (totalCount % pageSize != 0 ? 1 : 0);
        if(totalCount == 0){
            if(pageIndex != 1)
                throw new IndexOutOfBoundsException("Page index out of range.");
        } else
        if(pageIndex > pageCount)
            throw new IndexOutOfBoundsException("Page index out of range.");
    }

    public boolean isEmpty(){
        return totalCount == 0;
    }
}
